package com.levi.test.util;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.function.Supplier;

public class CommonUtils {

    /**
     * 异步执行+阻塞获取结果
     *
     * @param callable
     * @param <V>
     * @return
     */
    public static <V> FutureTask<V> executeAsyc(Callable<V> callable) {
        FutureTask<V> futureTask = new FutureTask(callable);
        new Thread(futureTask).start();
        return futureTask;
    }

    public static Supplier<Boolean> executeAsyc(Runnable runnable) {
        Thread thread = new Thread(runnable);
        thread.start();
        Supplier<Boolean> supplier = () -> {
            try {
                thread.join();
                return true;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        };
        return supplier;
    }

    public static void sleep(int s) {
        try {
            Thread.sleep(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
